/*
 * SiFiSy - A File System Simulator
 * Copyright (C) 2010  SevenSoft
 *
 * This file is part of SiFiSy.
 *
 * SiFiSy is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * SiFiSy is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with SiFiSy.  If not, see <http://www.gnu.org/licenses/>.
 */

package sifisy.connections;

import com.itextpdf.text.DocumentException;
import java.util.Vector;
import javax.swing.ImageIcon;
import sifisy.newgui.ProjectPanel;
import sifisy.core.Model;
import sifisy.newgui.MainWidget;
import sifisy.newgui.SifisyTreeNode;
import sifisy.newgui.SimulationPanel;

/**
 * Controller for ProjectPanel
 * @author Luca Zanini
 * @version %I%, %G%
 * @createData 12/05/2010
 */
public class ProjectManager extends Controller{
    private ProjectPanel panel;

    /**
     * Create a ProjectManager for the specified panel
     * @param projectName: name of the project
     * @param panel: the ProjectPanel object to control
     */
    public ProjectManager(String projectName, ProjectPanel panel) {
        this.panel = panel;
        panel.lockWindowsDialog(false);
        Model.newProject(projectName, panel);
    }

    /**
     * Add a simulation to the project
     * @param simulationId: the id of the simulation to add
     */
    public void addSimulation(int simulationId) {
        Model.getProject(panel.getProjectId()).addSimulation(simulationId);
    }

    /**
     * Remove a simulation from the project
     * @param simulationId: the id of the simulation to add
     */
    public void removeSimulation(int simulationId) {
        Model.getProject(panel.getProjectId()).removeSimulation(simulationId);
    }

    /**
     * Import an existing simulation into the project
     * @param filePath: the complete file path of the project to import
     */
    public int importSimulation(String filePath) {
        panel.lockWindowsDialog(false);
        

        String simName = "";
        SifisyTreeNode simNode = new SifisyTreeNode(simName);
        SimulationPanel simPanel = new SimulationPanel(simName, simNode, panel);
        panel.getTabbedPane().addTab("_sim",
                new ImageIcon(MainWidget.getMainWidgetFrame().getClass().getResource("icons/small/simulation.png")),
                simPanel);
        int tabIndex = panel.getTabbedPane().getTabCount() - 1;
        MainWidget.addNodeToTree(simNode, panel.getNode());
        simNode.setTabIndex(tabIndex);
        Model.openSimulation(filePath, simPanel);
        simPanel.refreshFields();
        Model.getProject(panel.getProjectId()).importSimulation(filePath, simPanel);

        simName = simPanel.getSimulationName();
        panel.getTabbedPane().setTitleAt(tabIndex, simName);
        //panel.getTabbedPane().setSelectedIndex(tabIndex);
        panel.getTabbedPane().updateUI();

        panel.lockWindowsDialog(true);

        return simPanel.getSimulationId();
    }

    /**
     * Start the compare of the project
     */
    public void startCompare() {
        Vector<String> errSimNames = new Vector<String>();
        for (int i=1; i<panel.getTabbedPane().getTabCount(); i++) {
            if ((((SimulationPanel)panel.getTabbedPane().getComponentAt(i)).getSimulationManager().hasErrors()) &&
                (Model.getProject(panel.getProjectId()).getCompare().contains(((SimulationPanel)panel.getTabbedPane().getComponentAt(i)).getSimId()))) {
                errSimNames.add(((SimulationPanel)panel.getTabbedPane().getComponentAt(i)).getSimulationName());
            }
        }

        if (errSimNames.size() == 0) {
            Model.getProject(panel.getProjectId()).compare();

            Model.getProject(panel.getProjectId()).compareOverhead();
            Model.getProject(panel.getProjectId()).compareFileSystem();
            Model.getProject(panel.getProjectId()).compareInternalFragmentation();
            Model.getProject(panel.getProjectId()).compareMaxLenghtFile();

            panel.showResults();
            panel.notifyPanel("");
        }
        else {
            String message = Model.translate("projectPanelErr01") + ":";

            for (int i=0; i<errSimNames.size(); i++) {
                message += ("\n  " + errSimNames.elementAt(i));
            }
            panel.notifyPanel(message);
        }

        panel.lockWindowsDialog(false);
    }

    /**
     * Add a simulation to the simulations to compare
     * @param simId: the id of the simulation to compare
     */
    public void addCompare(int simId) {
        Model.getProject(panel.getProjectId()).addCompare(simId);
    }

    /**
     * Remove a simulation to the simulations to compare
     * @param simId: the id of the simulation to remove
     */
    public void removeCompare(int simId) {
        Model.getProject(panel.getProjectId()).removeCompare(simId);
    }

    /**
     * Save the results of the project in pdf format
     * @param filePath: the complete file path of the pdf to save
     */
    public void exportPdf(String filePath) {
        try {
            Model.exportProjectPdf(panel, filePath);
        }
        catch (DocumentException e) {
            System.err.println(e.getMessage());
        }
    }
}
